package com.fas.hehe.system.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.permission.WildcardPermission;

import com.fas.core.BaseService;
import com.fas.hehe.system.model.Resource;
import com.jfinal.kit.StrKit;

public class ResourceService extends BaseService {
	// 得到资源对应的权限字符串
	public Set<String> findPermissions(Set<Long> resourceIds) {
		Set<String> permissions = new HashSet<String>();
		for (Long resourceId : resourceIds) {
			Resource resource = findOne(resourceId);
			if (resource != null
					&& !StringUtils.isEmpty(resource.getStr("permission"))) {
				permissions.add(resource.getStr("permission"));
			}
		}
		return permissions;
	}

	// 根据用户权限得到菜单
	public List<Resource> findMenus(Set<String> permissions) {
		List<Resource> allResources = findAll();
		List<Resource> menus = new ArrayList<Resource>();
		for (Resource resource : allResources) {
			if (resource.getLong("parent_id") == 0) {
				continue;
			}
			if (!resource.getStr("type").equals("menu")) {
				continue;
			}
			if (!hasPermission(permissions, resource)) {
				continue;
			}
			menus.add(resource);
		}
		return menus;
	}

	public String findMenusHtml(Set<String> permissions) {
		StringBuffer sb = new StringBuffer();

		List<Resource> menus = this.findMenusByParentId(Long.valueOf(0l));
		if (CollectionUtils.isNotEmpty(menus)) {
			for (Resource res : menus) {
				this.findMenusByParentId(res.getLong("id"));
			}
		}
		return sb.toString();
	}

	public String buildMenus(Long parentId,String contentPath, Set<String> permissions) {
		StringBuffer html = new StringBuffer();
		List<Resource> menus = this.findMenusByParentId(parentId);

		if (CollectionUtils.isNotEmpty(menus)) {
			for (Resource res : menus) {

				if (!hasPermission(permissions, res)) {
					continue;
				}

				String menuName = res.getStr("name");
				Long menuId = res.getLong("id");
				String url = res.getStr("url");
				String itemClass = res.getStr("icon_class");
				if (StrKit.isBlank(url)) {
					url = "javascript:void(0)";
				}else{
					url = contentPath + url;
				}

				List<Resource> subMenus = this.findMenusByParentId(menuId);
				if (CollectionUtils.isNotEmpty(subMenus)) {
					html.append("<li class='").append("").append("'>");
					html.append("	<a class='dropdown-toggle'>");
					html.append("       <i class='").append(itemClass+"").append("'></i>");// 图标
					html.append("		<span class='menu-text'>").append(menuName)
							.append("</span>");
					html.append("   	<b class='arrow fa fa-angle-down'></b>");
					html.append("   </a>");
					html.append("   <b class='arrow'></b>");

					html.append("	<ul class='submenu'>");

					html.append(buildMenus(menuId,contentPath, permissions));

					html.append("   </ul>");
					html.append("</li>");
				} else {
					html.append("<li class='").append("").append("'>");
					html.append("<a target='main_frame' href='").append(url)
							.append("'>");
					html.append("<i class='").append(itemClass+"").append("'></i>");// 图标
					html.append("	<span class='menu-text'>").append(menuName)
							.append("</span>");
					html.append("</a><b class='arrow'></b>");
					html.append("</li>");
				}
			}
		}
		return html.toString();
	}

	public List<Resource> findMenusByParentId(Long parentId) {
		final String sql = "select * from sys_resource where parent_id = ? and type= ?";
		return Resource.dao.find(sql, parentId, "menu");
	}

	public Resource findOne(Long resourceId) {
		return Resource.dao.findById(resourceId);
	}

	public List<Resource> findAll() {
		final String sql = "select id, name, type, url, permission, parent_id, parent_ids, available from sys_resource order by concat(parent_ids, id) asc";
		return Resource.dao.find(sql);
	}

	private boolean hasPermission(Set<String> permissions, Resource resource) {
		if (StringUtils.isEmpty(resource.getStr("permission"))) {
			return true;
		}
		for (String permission : permissions) {
			WildcardPermission p1 = new WildcardPermission(permission);
			WildcardPermission p2 = new WildcardPermission(
					resource.getStr("permission"));
			if (p1.implies(p2) || p2.implies(p1)) {
				return true;
			}
		}
		return false;
	}
}
